package com.syezon.android.base.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;

import java.io.File;

/**
 * Created by Administrator on 2017/11/1.
 */

public class ApkUtil {

    public static String TAG = ApkUtil.class.getSimpleName();

    /**
     * 安装APK
     *
     * @param context
     * @param path
     */
    public static boolean installApk(Context context, String path) {
        try {
            File file = new File(path);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            setIntentDataAndType(context , intent,"application/vnd.android.package-archive", file, true);
//            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    public static Uri getUriForFile(Context context, File file) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = getUriForFile24(context, file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    public static Uri getUriForFile24(Context context, File file) {
        Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(context,
                context.getPackageName() + ".fileprovider",
                file);
        return fileUri;
    }


    public static void setIntentDataAndType(Context context,
                                            Intent intent,
                                            String type,
                                            File file,
                                            boolean writeAble) {
        if (Build.VERSION.SDK_INT >= 24) {
            intent.setDataAndType(getUriForFile(context, file), type);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (writeAble) {
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        } else {
            intent.setDataAndType(Uri.fromFile(file), type);
        }
    }

    /**
     * 通过包名，判断某应用是否已安装
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean checkInstall(Context context, String packageName) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
            if (packageInfo == null) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 通过包名，打开某个应用
     *
     * @param context
     * @param packageName
     */
    public static void openApp(Context context, String packageName) {
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
        if (intent != null) {
            context.startActivity(intent);
        }
    }


    /**
     * 获取APK文件信息
     *
     * @param context
     * @param filePath
     * @return
     */
    public static ApkInfo getApkInfo(Context context, String filePath) {
        ApkInfo apkInfo = new ApkInfo();
        try {
            // 获取APK路径
            File file = new File(filePath);
            // 纠错
            if (!file.exists() || !filePath.toLowerCase().endsWith(".apk")) {
                return null;
            }
            PackageInfo packageInfo = context.getApplicationContext().getPackageManager().getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
            if (packageInfo != null) {
                apkInfo.appName = filePath;
                apkInfo.pkgName = packageInfo.packageName;
                apkInfo.versionName = packageInfo.versionName;// 版本号
                apkInfo.versionCode = packageInfo.versionCode;// 版本码
            }
            // 记录文件大小
            apkInfo.size = file.length();
        } catch (Exception e) {
            return null;
        }
        return apkInfo;
    }

    /**
     * APK安装包信息
     */
    public static class ApkInfo {

        // 基本属性
        public String appName;
        public String pkgName;
        public String apkName;
        public Bitmap appIcon;
        public String versionName;
        public int versionCode;
        public long size;
    }
}
